package com.ultrawise.survey.module.user.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.ultrawise.survey.core.Page;
import com.ultrawise.survey.core.PaginationHelper;
import com.ultrawise.survey.core.base.BaseDao;
import com.ultrawise.survey.core.base.Saveable;
import com.ultrawise.survey.module.user.vo.TSurveyInfoUsers;

@SuppressWarnings("all")
@Repository("userDao")
public class UsersDao extends BaseDao {

	// 根据条件查询表中的数据
	public Page<TSurveyInfoUsers> getUsersInfo(Map<String, String> map,
			Saveable objForSave, final Class obj, final int pageNo,
			final int pageSize) throws SQLException {
		PaginationHelper<TSurveyInfoUsers> ph = new PaginationHelper<TSurveyInfoUsers>();
		String sql = "from t_survey_info_users a where ac_IsOpen = 1 ";
		Object[] params = new Object[7];
		int index = 0;
		for (Iterator keys = map.keySet().iterator(); keys.hasNext();) {
			String key = (String) keys.next();
			if (StringUtils.isEmpty(key) || StringUtils.isEmpty(map.get(key))) {
				continue;
			}
			if (key.contains("Code")) {
				sql += " and b." + key + " = ? ";
				params[index] = map.get(key);
				index++;
			} else {
				sql += " and b." + key + " like ? ";
				params[index] = "%" + map.get(key) + "%";
				index++;
			}
		}
		Object[] paramsEnd = new Object[index];
		System.arraycopy(params, 0, paramsEnd, 0, index);
		return ph.fetchPage(getJdbcTemplate(), "select count(*) " + sql,
				"select * " + sql, paramsEnd, pageNo, pageSize,
				new BeanPropertyRowMapper(TSurveyInfoUsers.class));
	}

	// 根据条件查询用户表中的数据
	public Page getObjectList(Map<String, String> map, Saveable objForSave,
			final Class obj, final int pageNo, final int pageSize)
			throws SQLException {
		PaginationHelper ph = new PaginationHelper();
		String sql = "from " + objForSave.getTableName()
				+ " where 1=1 and issupper != 0";
		Object[] params = new Object[7];
		int index = 0;
		for (Iterator keys = map.keySet().iterator(); keys.hasNext();) {
			String key = (String) keys.next();
			if (StringUtils.isEmpty(key) || StringUtils.isEmpty(map.get(key))) {
				continue;
			}
			if (key.contains("Code")) {
				sql += " and " + key + " = ? ";
				params[index] = map.get(key);
				index++;
			} else {
				sql += " and " + key + " like ? ";
				params[index] = "%" + map.get(key) + "%";
				index++;
			}
		}
		Object[] paramsEnd = new Object[index];
		System.arraycopy(params, 0, paramsEnd, 0, index);
		return ph.fetchPage(getJdbcTemplate(), "select count(*) " + sql,
				"select * " + sql, paramsEnd, pageNo, pageSize,
				new BeanPropertyRowMapper(obj));
	}

	public Page<TSurveyInfoUsers> getUsersPages(TSurveyInfoUsers dto, int pageNo, int pageSize) {
		PaginationHelper<TSurveyInfoUsers> ph = new PaginationHelper<TSurveyInfoUsers>();
		String sql = " from " + dto.getTableName() + " where 1=1";
		Object[] params = new Object[7];
		int index = 0;
		if (!StringUtils.isEmpty(dto.getUs_realname())) {
			sql += " and US_REALNAME like  ? ";
			params[index] = "%" + dto.getUs_realname() + "%";
			index++;
		}
		Object[] paramsEnd = new Object[index];
		System.arraycopy(params, 0, paramsEnd, 0, index);

		return ph.fetchPage(getJdbcTemplate(), "select count(*) " + sql,
				"select * " + sql, paramsEnd, pageNo, pageSize,
				new BeanPropertyRowMapper<TSurveyInfoUsers>(TSurveyInfoUsers.class));
	}

	/*****
	 * 添加urser用户白哦信息
	 * 
	 * @param user
	 * @return
	 */
	public TSurveyInfoUsers addUsers(final TSurveyInfoUsers user) {
		super.saveAndReturnKey(user).intValue();
		return user;
	}

	/**
	 * 判断用户是否存在
	 */
	public boolean isExist(String strLogin) {
		String sql = "SELECT COUNT(*) AS Count FROM T_SURVEY_INFO_USERS WHERE uslogin='"
				+ strLogin + "'";
		int con = getJdbcTemplate().queryForInt(sql);
		boolean flag = false;
		if (con > 0) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;

	}

	/**
	 * 删除一个用户
	 */
	public boolean delUser(String id) {
		String sql = "update  T_SURVEY_INFO_USERS set usStatus=0  where id='" + id
				+ "'";
		int count = getJdbcTemplate().update(sql);
		boolean flag = false;
		if (count > 0) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;

	}

	/**
	 * 查询用户
	 */

	public List findUser(String colunm, String value, Saveable objForSave,
			Class obj) {
		String sql = "select * from " + objForSave.getTableName() + " where "
				+ colunm + "  not like  ?";
		return getJdbcTemplate().query(sql, new BeanPropertyRowMapper(obj),
				value + "%");

	}

	/**
	 * 对用户进行模糊查询
	 */
	public List GetUser(String colunm, String value, Saveable objForSave,
			Class obj) {
		String sql = "select * from " + objForSave.getTableName() + " where "
				+ colunm + "=?";
		return getJdbcTemplate().query(sql, new BeanPropertyRowMapper(obj),
				value);

	}

	/**
	 * 模糊查询当即全部用户
	 */
	public List findAllUser(String colunm, String value, Saveable objForSave,
			Class obj) {
		String sql = "select * from " + objForSave.getTableName() + " where "
				+ colunm + "  like  ?";
		return getJdbcTemplate().query(sql, new BeanPropertyRowMapper(obj),
				value + "%");

	}

}
